<template>
    <div class="login">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="loginForm">
            <el-form-item label="用户名" prop="username" color="#ccc">
                <el-input v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password" color="#ccc">
                <el-input v-model="loginForm.password" show-password></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="login" class="but">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { userLogin } from '../request/api'
export default {
    data() {
        return {
            loginForm: {
                username: '',
                password: ''
            },
            //    规则验证
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        login() {
            this.$refs.loginForm.validate(flag => {
                if (flag) {
                    // console.log(this.loginForm, '表单对象');
                    userLogin(this.loginForm)
                        .then(res => {
                            if (res.data.code == 200) {
                                this.$message.success(res.data.msg)
                                // 把结果存储到vuex中
                                //触发行动
                                this.$store.dispatch('changeUserAction', res.data.list)
                                this.$router.push('/index')
                            }else{
                                 this.$message.error(res.data.msg)
                            }
                        })
                } else {
                    console.log(验证失败);
                }
            })
        }
    }
};
</script>

<style scoped>
.login {
    width: 100vw;
    height: 100vh;
    background: linear-gradient(rgb(197, 163, 138), rgb(99, 84, 62));
}

.login .loginForm {
    position: absolute;
    width: 400px;
    height: 200px;
    left: 45%;
    top: 60%;
    margin: -165px 0 0 -180px;
    color: white;
    /* background: white;     */
}

.login .loginForm .but {
    margin-left: 110px;
}

.el-form-item__label {
    color: white;
}
</style>
